package control;

import javax.microedition.lcdui.Graphics;

import main.Factory;
import main.Res;

import gameObject.Hero;
import gameObject.item.ItemArm;
import gameObject.item.ItemSuit;

public class HeroDisplayPanel extends Control {
	protected int x, y, w, h;
	Hero hero;
	BolderButton armButton;
	BolderButton suitButton;
	TextControl infoText;
	int margin = 10;

	public HeroDisplayPanel(int x, int y, int w, int h) {
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
		hero = new Hero();
		int bW = 40;
		int pW = (h - 40-2*margin);
		hero.setPosition(x + margin + pW / 2, y + margin + pW / 2);
		armButton = new BolderButton(14, x + margin, y + margin + pW, bW, bW);
		suitButton = new BolderButton(15, x + bW + margin, y + margin + pW, bW,	bW);
		infoText = new TextControl("ATT : 100\nDEF : 100\nSP  : 100\nRA  :100",
				3 * margin + pW, y + margin);
	}

	public void setCommandArm(ControlCommand command) {
		armButton.setCommand(command);
	}

	public void setCommandSuit(ControlCommand command) {
		suitButton.setCommand(command);
	}

	public void setHeroInfo(Hero hero) {
		infoText.setText(hero.getInfoString());

		if (hero.getArm() == null) {
			this.hero.setArm(null);
			armButton.setIcon(14);
		} else {
			this.hero.setArm((ItemArm) Factory
					.createItem(hero.getArm().getId()));
			armButton.setIcon(hero.getArm().getId() + Res.ITEM_ICON_OFFSET);
		}

		if (hero.getSuit() == null) {
			this.hero.setSuit(null);
			suitButton.setIcon(15);
		} else {
			this.hero.setSuit((ItemSuit) Factory.createItem(hero.getSuit()
					.getId()));
			suitButton.setIcon(hero.getSuit().getId() + Res.ITEM_ICON_OFFSET);
		}
	}

	public boolean pointerPressed(int x, int y) {
		armButton.pointerPressed(x, y);
		suitButton.pointerPressed(x, y);
		return false;
	}

	public boolean pointerReleased(int x, int y) {
		armButton.pointerReleased(x, y);
		suitButton.pointerReleased(x, y);
		return false;
	}

	protected void controlPaint(Graphics g) {

		// TODO Auto-generated method stub
		// super.controlPaint(g);
		g.setColor(COLOR_BACK);
		g.fillRect(x, y, w, h);
		g.setColor(COLOR_FORE);
		g.drawRect(x - 1, y - 1, w + 1, h + 1);

		int pW = (h - 50);
		g.setColor(COLOR_PRESSED);
		g.fillRect(x + margin, y + margin, pW, h - 2 * margin);
		g.setColor(COLOR_FORE);
		g.drawRect(x + margin - 1, y + margin - 1, pW + 1, h - 2 * margin + 1);

		hero.paint(g);
		armButton.paint(g);
		suitButton.paint(g);
		infoText.paint(g);
	}
}
